package com.lee.transactionaldemo.service.mandatory;

import com.lee.transactionaldemo.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

/**
 * @author ：liylw
 * @date ：Created in 2021/4/23 11:13
 */
@Service
public class Mandatory1 {
    @Autowired
    private UserService userService;

    @Autowired
    private Mandatory2 mandatory2;


    /**
     * 抛出异常org.springframework.transaction.IllegalTransactionStateException: No existing transaction found for transaction marked with propagation 'mandatory'
     */
    public void test1(){
        mandatory2.testSuccess();
        userService.saveUserASuccess();
    }


    /**
     * 抛出异常org.springframework.transaction.IllegalTransactionStateException: No existing transaction found for transaction marked with propagation 'mandatory'
     */
    @Transactional(propagation = Propagation.SUPPORTS,rollbackFor = Exception.class)
    public void test2(){
        mandatory2.testSuccess();
        userService.saveUserASuccess();
    }

    /**
     * 正常写入
     */
    @Transactional(propagation = Propagation.REQUIRED,rollbackFor = Exception.class)
    public void test3(){
        mandatory2.testSuccess();
        userService.saveUserASuccess();
    }


    /**
     * 全部回滚
     */
    @Transactional(propagation = Propagation.REQUIRED,rollbackFor = Exception.class)
    public void test4(){

        userService.saveUserASuccess();
        mandatory2.testFail();
    }
}
